/*===========================================

	True Love Wii

	Coders:
		GizmoTheGreen
		TalkLittle

	Credits:
		NoNameNo

============================================*/
//#include <stdio.h>
//#include <stdlib.h>
//#include <math.h>
//#include <gccore.h> // all above is in grrlib.h
//#include <sysdir.h> //not needed

#include <ogcsys.h>
#include <wiiuse/wpad.h>

#include "common.h"
#include "script.h"
// TODO: move oggplayer include to script.c?
#include "ogg/oggplayer.h"
//#include "gamelib/game.h"

Mtx GXmodelView2D;

// Shared "Global" variables
int endianness;
int quit;
FILE *MRSfp;
int MRS_paletteNumber; // 0x0-0xF
u8 *MRS_bg_data = NULL;
u8 *MRS_sprite_data = NULL;
u32 MRS_bg_colorTable[256]; // 256 or 16??
u32 MRS_sprite_colorTable[256]; // 256 or 16??
short MRS_bg_width, MRS_bg_height;

// Static "Global" variables
int res;
u32 type;
u32 wpaddown;
ir_t IR1;
orient_t orient;

#define FONTCOL 0xFF000000;
#define MRSfile "sd:/data/truelove/MRS" 
// I'd prefer this, i hope more developers will put their folders in "data" di
// so we dont get 20 diff folders in root (bad for organisation)

// functions
void VBL(void); // waitvsync replacement :)

void OGG_Play(const char *filename);

void Debug(const char * text);

// MAIN!!!
int main()
{
	// These should only remain until main is over :) fade;
	//u32 fade;

	// General Video and Controller Inits
	GRRLIB_Init();
	WPAD_Init();
	// FAT Init
	fatInitDefault();

	endianness = get_endianness();

	// show grrlib intro
	// GRRLIB_Intro();

	// Only need to open MRS once, and reuse often
	MRSfp = fopen(MRSfile, "rb");

	MRS_paletteNumber = 0;


	Debug("Loading Textures\n");
	// Some textures
	//u8 *tex_font = GRRLIB_LoadTextureFromFile("sd:/data/graphics/font.png");
	//u8 *tex_cursor = GRRLIB_LoadTextureFromFile("sd:/data/graphics/cursor.png");
	//u8 *tex_bg_Main = GRRLIB_LoadTextureFromFile("sd:/data/graphics/title.png");
	//u8 *tex_win_Menu = GRRLIB_LoadTextureFromFile("sd:/data/graphics/mainmenu.png");
	//u8 *tex_btn_NewGame = GRRLIB_LoadTextureFromFile("sd:/data/graphics/start.png");
	//u8 *tex_btn_Continue = GRRLIB_LoadTextureFromFile("sd:/data/graphics/continue.png");

	// some other setups
	Debug("Set WPAD to IR\n");
	WPAD_SetDataFormat(WPAD_CHAN_0, WPAD_FMT_BTNS_ACC_IR);


	// Girl animation...?



	//OGG_Play("sd:/data/music/m011.ogg");



	// Main menu code
	quit = 0;
	while(!quit)
	{
		readScript("MAIN.DAT", 0);

		VBL();
	}

	// Clear allocated memory
	fclose(MRSfp);
	GRRLIB_Exit();
	return 0;
}

void VBL() // waitvsync replacement :)
{
	// Wiimote stuff
	WPAD_IR(WPAD_CHAN_0, &IR1);
	WPAD_Orientation(WPAD_CHAN_0, &orient);
	WPAD_ScanPads();
	// Detect buttons
	wpaddown = WPAD_ButtonsDown(0);
	// Quit
	if (wpaddown & WPAD_BUTTON_HOME)
	{
		quit = 1;
	}
	// Check wiimote stat
	res = WPAD_Probe(WPAD_CHAN_0, &type);
	GRRLIB_Render();
}

void OGG_Play(const char *filename)
{
	fatInitDefault();
	SND_Init(INIT_RATE_48000);
	FILE* ogg;
	ogg = fopen (filename,"r");
	if(ogg)
	{
		fclose(ogg);
		int fd;
		fd=open(filename,O_RDONLY, 0);
		PlayOgg(fd,0,OGG_INFINITE_TIME);
	}
}

void Debug(const char * text)
{
	FILE * debug;
	debug = fopen("sd:/debug.txt", "a");
	fputs (text,debug);
	fclose(debug);
}
